﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace WPFServices.Models.AI
{
    /// <summary>
    /// 聊天消息类型
    /// </summary>
    public enum MessageType
    {
        User,
        Assistant,
        System
    }

    /// <summary>
    /// 聊天消息模型
    /// </summary>
    public class ChatMessage : INotifyPropertyChanged
    {
        private string _content = string.Empty;
        private bool _isStreaming = false;

        public string Id { get; set; } = Guid.NewGuid().ToString();
        public MessageType Type { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.Now;

        public string Content
        {
            get => _content;
            set
            {
                _content = value;
                OnPropertyChanged(nameof(Content));
            }
        }

        public bool IsStreaming
        {
            get => _isStreaming;
            set
            {
                _isStreaming = value;
                OnPropertyChanged(nameof(IsStreaming));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    /// <summary>
    /// AI请求DTO - 匹配后端接口
    /// </summary>
    public class AiRequestDto
    {
        [JsonPropertyName("message")]
        public string Message { get; set; } = string.Empty;

        [JsonPropertyName("userId")]
        public string UserId { get; set; } = "user_test001";

        [JsonPropertyName("conversationId")]
        public string ConversationId { get; set; } = string.Empty;
    }

    /// <summary>
    /// 流式响应数据块
    /// </summary>
    public class StreamChunk
    {
        [JsonPropertyName("type")]
        public string Type { get; set; } = string.Empty;

        [JsonPropertyName("content")]
        public string Content { get; set; } = string.Empty;

        [JsonPropertyName("conversationId")]
        public string ConversationId { get; set; } = string.Empty;

        [JsonPropertyName("timestamp")]
        public DateTime Timestamp { get; set; }
    }

    /// <summary>
    /// 回调模式响应
    /// </summary>
    public class CallbackResponse
    {
        [JsonPropertyName("success")]
        public bool Success { get; set; }

        [JsonPropertyName("response")]
        public string Response { get; set; } = string.Empty;

        [JsonPropertyName("conversationId")]
        public string ConversationId { get; set; } = string.Empty;

        [JsonPropertyName("duration")]
        public double Duration { get; set; }

        [JsonPropertyName("chunks")]
        public List<StreamChunk> Chunks { get; set; } = new();

        [JsonPropertyName("error")]
        public string Error { get; set; } = string.Empty;
    }
}